<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 个人信息 -->
    <van-cell
      class="avatar-cell"
      title="头像"
      is-link
      center
      @click="$refs.file.click()"
    >
      <van-image class="avatar" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      @click="isUserShow = true"
      :value="user.name"
      is-link
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      @click="isUserShowSex = true"
      is-link
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      @click="isUserShowBirthday = true"
      is-link
    />
    <!-- /个人信息 -->

    <!-- 头像 -->
    <input type="file" name="" @change="onFileChange" ref="file" hidden />
    <van-popup
      class="userbgc"
      v-model="isShowPhooto"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <updatePhoto v-if="isShowPhooto" :img="img" @delPhoto="delPhoto" />
    </van-popup>
    <!-- /头像 -->

    <!-- 编辑个人信息 -->
    <van-popup
      class="userbgc"
      v-model="isUserShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <updateName
        v-if="isUserShow"
        :value="user.name"
        @delUserShow="delUserShow"
      />
    </van-popup>
    <!-- /编辑个人信息 -->

    <!-- 修改性别 -->
    <van-popup
      v-model="isUserShowSex"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <updateSex :value="user.gender" />
    </van-popup>
    <!-- /修改性别 -->

    <!-- 修改生日 -->
    <van-popup
      v-model="isUserShowBirthday"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <updateBirthday :value="user.birthday" />
    </van-popup>
    <!-- /修改生日 -->
  </div>
</template>

<script>
import { getUserFile } from "@/api/user.js";
import updateName from "./update-name.vue";
import updateSex from "./update-sex.vue";
import updateBirthday from "./update-birthday.vue";
import updatePhoto from "./update-photo.vue";
export default {
  components: {
    updateName,
    updateSex,
    updateBirthday,
    updatePhoto,
  },

  props: {},
  data() {
    return {
      user: {},
      isUserShow: false,
      //性别
      isUserShowSex: false,
      isUserShowBirthday: false,
      // 头像图片
      isShowPhooto: false,
      img: "",
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getUser();
    this.$bus2.$on("delUserSex", this.emitSex);
    this.$bus2.$on("delBirthday", this.emitBirthday);
  },
  mounted() {},
  methods: {
    async getUser() {
      try {
        const { data: res } = await getUserFile();
        this.user = res.data;
      } catch (error) {
        this.$toast("获取数据失败");
      }
    },
    // 关闭修改框
    delUserShow(flag) {
      this.isUserShow = false;

      if (flag) {
        this.getUser();
      }
    },
    // 关闭修改性别
    emitSex(flag) {
      this.isUserShowSex = false;
      if (flag) {
        this.getUser();
      }
    },
    // 关闭修改日期
    emitBirthday(flag) {
      this.isUserShowBirthday = false;
      if (flag) {
        this.getUser();
      }
    },
    onFileChange() {
      const file = this.$refs.file.files[0];
      this.img = window.URL.createObjectURL(file);
      this.isShowPhooto = true;
      this.$refs.file.value = "";
    },
    // 关闭修改头像
    delPhoto(flag) {
      this.isShowPhooto = false;
      if (flag) {
        this.getUser();
      }
    },
  },
  beforeDestroy() {
    this.$bus2.$off("delUserSex");
    this.$bus2.$off("delBirthday");
  },
};
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
  .userbgc {
    background-color: rgb(245, 247, 249);
  }
}
</style>